<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class articulos extends MY_Controller {

	public function _remap($method){
		$this->carabiner->css(array(
				array('base.css', 'screen')
		));
		$this->carabiner->js(array(
				array('libs/jquery.min.js'),
		));

    if($this->session->userdata('id_usuario'))
    {
      if ($this->session->userdata['acceso'] === 'admin' || $this->session->userdata['tipo'] === 'gratuito' || $method == 'twitter' || $method == 'ajax_get_articulo_info')
        $this->{$method}();
      else
        redirect(base_url('tic/'));
    }
    else
      redirect(base_url());
	}


	public function index()
	{
		$this->carabiner->css(array(
			array('libs/jquery.tinyscroll.css', 'screen'),
			array('libs/jquery.smoothDivScroll.css', 'screen'),
			array('libs/jquery.slider.css', 'screen'),
			array('articulos.css', 'screen'),
			array('barra_rfree.css', 'screen')
		));
		$this->carabiner->js(array(
				array('libs/jquery.html5placeholder.js'),
				array('libs/jquery.tinyscrollbar.min.js'),
				array('libs/jquery-ui.js'),
				array('libs/jquery.mousewheel.min.js'),
				array('libs/jquery.smoothdivscroll-1.2-min.js'),
				array('libs/jquery.slides.min.js'),
				array('articulo/articulo.js'),
				array('free/barra_rfree.js')
		));

		$this->load->model('skin_model');
		$params['artic_full'] = $this->skin_model->accesoUser($this->input->get('art'));

		$params['arti'] = $this->db->query("SELECT a.id_articulo, a.id_tipo, a.numero,
				a.titulo, a.descripcion, a.fecha_creacion, a.imagen1, a.imagen2, a.imagen3, at.nombre
			FROM articulos AS a INNER JOIN articulos_tipo AS at ON a.id_tipo = at.id_tipo
			WHERE a.id_articulo = ".$this->input->get('art'))->row();


		$params['mas_artic'] = $this->db->query("SELECT id_articulo, numero, titulo, imagen1, fecha_creacion
			FROM articulos WHERE status = 1 AND id_tipo = ".$params['arti']->id_tipo." ORDER BY numero DESC")->result();

		$params['ventas'] = $this->skin_model->getAsesoriaVenta();


		$params['seo'] = array('titulo' => $params['arti']->titulo.' - Tienda Capp');


		if ($this->session->userdata('tipo') == 'tic') {
			$params['barra_r'] = $this->load->view('skin/free/barra_right_free', $params, true);
		}else{
			$params['barra_r'] = $this->load->view('skin/free/barra_right_free', $params, true);
		}

		$this->load->view('skin/header', $params);
		$this->load->view('skin/articulos/articulos', $params);
		$this->load->view('skin/footer');
	}

	public function pdf(){
		$this->load->model('skin_model');
		$params = $this->skin_model->accesoUser($this->input->get('art'));

		$arti = $this->db->query("SELECT id_articulo, id_tipo, url_archivo
			FROM articulos
			WHERE id_articulo = ".$this->input->get('art'))->row();

		if (isset($arti->id_articulo)) {
			if ($arti->url_archivo != '') {
				switch ($arti->id_tipo) {
					case 1:
						$pagecount = 3;
						break;
					case 2:
						$pagecount = 1;
						break;
					default:
						$pagecount = 0;
						break;
				}

				if ($pagecount > 0) {
					require_once(APPPATH.'libraries/fpdi/fpdf_tpl.php');
					require_once(APPPATH.'libraries/fpdi/fpdi.php');
					// initiate FPDI
					$pdf = new FPDI();
					$num_pags = $pdf->setSourceFile($arti->url_archivo);

					if ($params->articulo == 'tic') {
						$pagecount = $num_pags;
					}

					for($i=0; ($i<$pagecount && $i<$num_pags); $i++){
						$pdf->AddPage();
						$tplidx = $pdf->importPage($i+1);
						$pdf->useTemplate($tplidx, 0, 0, 200);
					}


					$name = explode('/', $arti->url_archivo);
					$pdf->Output($name[count($name)-1], ($this->input->get('show')!=''? 'I': 'D'));
				}else{
					$file = explode('/', $arti->url_archivo);
					header("Content-Disposition: attachment; filename=".$file[count($file)-1]);
					readfile($arti->url_archivo);
				}
			}
		}
	}


	public function sendmail(){
		$arti = $this->db->query("SELECT id_articulo, id_tipo, titulo, url_archivo
			FROM articulos
			WHERE id_articulo = ".$this->input->post('art'))->row();

		$html = '<p><strong>'.$this->session->userdata('nombre').'</strong> te invito a leer el articulo '.$arti->titulo.'</p>';
		Email::base($html);
		Email::send($this->input->post('email'), 'Invitacion a la Tienda Capp');
		redirect(base_url('articulos?art='.$this->input->post('art')));
	}

	public function faqs(){
		$this->load->view('skin/home/faqs');
	}

	public function about(){
		$this->load->view('skin/home/about');
	}

  public function ajax_get_articulo_info()
  {
    $this->load->model('articulos_model');
    $res = $this->articulos_model->get_articulo_info($_GET['id']);

    echo json_encode($res);
  }

  public function twitter()
  {
    $this->load->library('my_twitter');
    $this->my_twitter->set_oauth_callback(base_url('articulos/twitter/?id='.$_GET['id']));
    $res = $this->my_twitter->oauth();


    $this->load->model('articulos_model');
    $info = $this->articulos_model->get_articulo_info($_GET['id']);

    if (strlen($info['info'][0]->titulo) > 92)
      $info['info'][0]->titulo = substr($info['info'][0]->titulo, 0, 85) . "...";

    $message = "Te recomiendo el artículo “".$info['info'][0]->titulo."” entra => ".base_url('publico/a/?id='.$_GET['id']);
    $result = $this->my_twitter->statuses_update($message);
    echo("<script>window.close();</script>");
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */